package com.cloud.animationtestdemo.strage

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.graphics.Matrix
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator


/**
 * @author: njb
 * @date:   2024/12/23 23:02
 * @desc:   缩放动画策略类
 */
class ScaleAnimationStrategy : BaseAnimationStrategy {
    private val scaleXProperty = PropertyValuesHolder.ofFloat("scaleX", 1f)
    private val scaleYProperty = PropertyValuesHolder.ofFloat("scaleY", 1f)

    override fun getAlphaForProgress(progress: Float): Float {
        // 可以让透明度和缩放比例有一定关联，比如缩放越大越透明等
        return 1 - progress * 0.5f
    }

    fun animateView(view: View, startScaleX: Float, startScaleY: Float, endScaleX: Float, endScaleY: Float, duration: Long) {
        scaleXProperty.setFloatValues(startScaleX, endScaleX)
        scaleYProperty.setFloatValues(startScaleY, endScaleY)

        val animator = ValueAnimator.ofPropertyValuesHolder(scaleXProperty, scaleYProperty)
        animator.duration = duration
        animator.addUpdateListener { animation ->
            val currentScaleX = (animation as? ValueAnimator)?.getAnimatedValue(scaleXProperty.toString()) as? Float?: endScaleX
            val currentScaleY = (animation as? ValueAnimator)?.getAnimatedValue(scaleYProperty.toString()) as? Float?: endScaleY
            view.scaleX = currentScaleX
            view.scaleY = currentScaleY
            val progress = animation.animatedFraction
            val alpha = getAlphaForProgress(progress)
            view.alpha = alpha
        }
        animator.start()
    }
}
